package com.example.thirdhomework

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View
import android.widget.Button;
import android.widget.ImageView;


class FourthActivity : AppCompatActivity() {
    var imageView_photo: ImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fourth)
        if (shouldAskPermissions()) {
            askPermissions()
        }

        val take: Button = findViewById(R.id.take)
        imageView_photo = findViewById<ImageView>(R.id.imageView2)

        take.setOnClickListener{
            val intent = Intent()
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(intent, Companion.REQUEST_CAMERA)
        }

    }

    protected fun shouldAskPermissions(): Boolean {
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1)
    }

    protected fun askPermissions() {
        val permissions = arrayOf(
            "android.permission.CAMERA"
        )
        val requestCode = 200
        requestPermissions(permissions, requestCode)
    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        @Nullable data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) return
        val bundle = data.extras
        if (requestCode == Companion.REQUEST_CAMERA) {
            val b = bundle!!["data"] as Bitmap?
            imageView_photo!!.setImageBitmap(b)
        }
    }
    companion object {
        private const val REQUEST_CAMERA = 3
    }

}